package leecode

import "strings"

func mostCommonWord(paragraph string, banned []string) string {
	bannedStr := strings.Join(banned, " ")
	//替换字符串中的标点符号
	paragraph = strings.Replace(paragraph, "!", " ", -1)
	paragraph = strings.Replace(paragraph, "?", " ", -1)
	paragraph = strings.Replace(paragraph, ",", " ", -1)
	paragraph = strings.Replace(paragraph, "'", " ", -1)
	paragraph = strings.Replace(paragraph, ";", " ", -1)
	paragraph = strings.Replace(paragraph, ".", " ", -1)
	//全部转为小写
	paragraph = strings.ToLower(paragraph)
	paragraphArr := strings.Split(paragraph, " ")
	wordMap := make(map[string]int, len(paragraphArr))
	for i := 0; i < len(paragraphArr); i++ {
		//原字符串 , 需要查找的字符串
		if !strings.Contains(bannedStr, paragraphArr[i]) {
			wordMap[paragraphArr[i]] += 1
		}
	}
	maxCount := 0
	var result string
	for key, value := range wordMap {
		if value > maxCount {
			maxCount = value
			result = key
		}
	}
	return result
}
